package com.kinderas.validators
{
	/**
	 * Textvalidators are a set of static validators for validating
	 * text. 
	 * @author Jørn Kinderås
	 * 
	 */	
	public class TextValidator
	{
		/**
		 * The function takes a string and test if that string is a valid
		 * email address. Returns true if it is, otherwise false.
		 * Usage:
		 * <listing>var myEmail:String = "jornki&#64;me.com"; 
		 * trace(TextExtensions.validateEmail(myEmail));</listing>
		 * 
		 * @param email
		 * @return boolean
		 * 
		 */		
		public static function validateEmail(email:String):Boolean{
			var appr1:Boolean = false;
			var appr2:Boolean = false;
			var bannedChars:Array = new Array('?','/','#');
			for (var i:int=0; i<email.length; i++) {
				//Check for banned chars
				for (var j:int=0; j<bannedChars.length; j++) {
					if (email.charAt(i) == bannedChars[j]) {
						return false;
					}
				}
				if (email.charAt(i) == '.' && (email.substr(i,email.length).length > 2)) {
					appr1 = true;
				}
				if (email.charAt(i) == '@') {
					appr2 = true;
				}
			}
			if (appr1 && appr2) {
				return true;
			}
			return false;
		}
	}
}